﻿using DICOMViewer.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using xDataSet;

namespace DICOMViewer.Utility
{
    public class RequisitionDataHelper
    {
        private static RequisitionDataHelper defaultInstance;

        public static RequisitionDataHelper Default
        {
            get { return defaultInstance ?? (defaultInstance = new RequisitionDataHelper()); }
        }
        static readonly List<Model.Requisition> emptyList = new List<Model.Requisition>();
        public static List<Requisition> Retrive()
        {
            try
            {
                IncoolDicomViewerBase.IncoolDicomBaseClass.G_xDataSetConfigInterface.Token =
                    Model.LoginInfo.Current?.Token;
                xDataSet.xDataSet mRet = new xDataSet.xDataSet("Weixin_Interface_User", "viewdata", "getViewUserData",
                    new xParameter[]
                    {
                        new xParameter("uuid", Model.LoginInfo.Current?.Uuid),
                        new xParameter("filter", string.Empty)
                    },
                    IncoolDicomViewerBase.IncoolDicomBaseClass.G_xDataSetConfigInterface);
                if (mRet.SretSuccess)
                {
                    List<Requisition> list =
                        Utility.SerializeHelper.DeserializeWithJson<List<Requisition>>(
                            mRet.SRetJObject["body"].ToString());
                    if (list != null)
                    {
                        return list;
                    }
                    else
                    {
                        MessageBox.Show(mRet.SRetJObject["message"].ToString());
                    }
                }
                else
                {
                    MessageBox.Show("与服务器通讯失败！请检查网络连接后重试！");
                }
                return emptyList;
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("发生错误，错误信息如下：{0}，错误源：{1}",ex.Message,ex.StackTrace));
                return emptyList;
            }}
        public static List<Requisition> Retrive(string uuid)
        {
            try
            {
                IncoolDicomViewerBase.IncoolDicomBaseClass.G_xDataSetConfigInterface.Token =
                    Model.LoginInfo.Current.Token;
                IncoolDicomViewerBase.IncoolDicomBaseClass.G_xDataSetConfigInterface.UserID =
                    Model.LoginInfo.Current.Uuid;
                xDataSet.xDataSet mRet = new xDataSet.xDataSet("Weixin_Interface_User", "viewdata", "getUserDataListByUUID",
                    new xParameter[]
                    {
                        new xParameter("uuid",uuid)
                    },
                    IncoolDicomViewerBase.IncoolDicomBaseClass.G_xDataSetConfigInterface);
                if (mRet.SretSuccess)
                {
                    List<Requisition> list =
                        Utility.SerializeHelper.DeserializeWithJson<List<Requisition>>(
                            mRet.SRetJObject["body"].ToString());
                    if (list != null)
                    {
                        return list;
                    }
                    else
                    {
                        MessageBox.Show(mRet.SRetJObject["message"].ToString());
                    }
                }
                else
                {
                    MessageBox.Show("与服务器通讯失败！请检查网络连接后重试！");
                }
                return emptyList;
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("发生错误，错误信息如下：{0}，错误源：{1}", ex.Message, ex.StackTrace));
                return emptyList;
            }

        }
    }
}